﻿window.Category = Backbone.Model.extend({
    defaults: {
        Name: '',
        Keywords: '',
        Description: ''
    }
});

window.ProductCategoryCollection = Backbone.Collection.extend({
    model: Category,
    urlRoot: '/api/productcatagory/',
    url: function () {
        if (this.level !== undefined)
            return this.urlRoot + this.level;
        else
            return this.urlRoot + this.nextID;
    }
});


window.FirstLevelCollection = ProductCategoryCollection.extend({
    level: 0
});

window.SecondLevelCollection = ProductCategoryCollection.extend({

});

window.ThirdLevelCollection = ProductCategoryCollection.extend({

});

window.CategoryView = Backbone.View.extend({
    renderNext: function (e) {
        var target = $(e.currentTarget);

        var clickModel = _.find(this.collection.toArray(), function (model) {
            return model.get('ID') == target.attr('nextid');
        });

        var level = clickModel.get('CategoryLevel');

        localStorage.setItem('categorytitle' + level, clickModel.get('Name'));
        localStorage.setItem('nextid_' + level, target.attr('nextid'));

        if (target.attr('islast') === 'false') {
            pageView.goTo(this.NextPage);
        } else {
            localStorage.setItem('productcategoryid', target.attr('nextid'));
            localStorage.setItem('orderType', 'SaleNumber');
            localStorage.setItem('isASC', true);
            localStorage.setItem('pageIndex', 1);
            localStorage.setItem('pageCount', 5);

            pageView.goTo('ProductList');
        }
    }
});

window.FirstLevelView = CategoryView.extend({
    el: "#jqt",
    NextPage: 'SecondCategory',
    initialize: function () {
        this.collection = new FirstLevelCollection();
        this.template = $('#FirstCatagoryTemplate').html();
    },

    events: {
        'click .firstCategoryList li': 'renderNext'
    },

    render: function () {
        var that = this;
        var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(), innerFooter: $('#InnerFooterTemplate').html() };
        var data = { title: "全部分类", btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        this.collection.fetch({
            success: function () {
                var data = { products: that.collection.toJSON() };
                var template = $('#CategoryListTemplate').html();
                $('#FirstCategory .firstCategoryList').html(Mustache.render(template, data, []));
                pageView.resizeScroll();
            }
        });

        return this;
    }

});

window.SecondLevelView = CategoryView.extend({
    el: "#jqt",
    NextPage: 'ThirdCategory',
    initialize: function () {
        this.collection = new SecondLevelCollection();
        this.template = $('#SecondCatagoryTemplate').html();

    },
    events: {
        'click .secondCategoryList li': 'renderNext'
    },
    render: function () {
        var that = this;
        var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(), innerFooter: $('#InnerFooterTemplate').html() };
        var data = { hasBack: true, title: "全部分类", btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        this.collection.nextID = localStorage.getItem('nextid_1');

        this.collection.fetch({ success: function () {
            var data = { products: that.collection.toJSON() };
            var template = $('#CategoryListTemplate').html();
            $('#SecondCategory .secondCategoryList').html(Mustache.render(template, data, []));
            pageView.resizeScroll();
        }
        });

        return this;
    }
});

window.ThirdLevelView = CategoryView.extend({
    el: '#jqt',
    initialize: function () {
        this.collection = new ThirdLevelCollection();
        this.template = $('#ThirdCatagoryTemplate').html();
    },
    events: {
        'click .thirdCategoryList li': 'renderNext'
    },
    render: function () {
        var that = this;
        var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(), innerFooter: $('#InnerFooterTemplate').html() };
        var data = { hasBack: true, title: localStorage.getItem('categorytitle1'), btnListR: [{ name: 'sort' }, { name: 'cart' }, { name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        this.collection.nextID = localStorage.getItem('nextid_2');

        this.collection.fetch({
            success: function () {
                var data = { products: that.collection.toJSON() };
                var template = $('#CategoryListTemplate').html();
                $('#ThirdCategory .thirdCategoryList').html(Mustache.render(template, data, []));
                pageView.resizeScroll();
            }
        });

        return this;
    }
});